package com.mystnihon.tdroid.view.fragment;

import org.apache.commons.net.ftp.FTPFile;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockFragment;
import com.mystnihon.tdroid.R;
import com.mystnihon.tdroid.manager.ftp.FTPManager.FTPListener;
import com.mystnihon.tdroid.service.FTPService;
import com.mystnihon.tdroid.service.FTPService.BackgroundServiceBinder;

public class FtpFragment extends SherlockFragment implements FTPListener {
	Button mButton;
	String hostname;
	int port;

	public FtpFragment() {

	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		getActivity().startService(new Intent(getActivity(), FTPService.class));
		// mFTPClient = new FTPManager(getActivity());
		// mFTPClient.setFTPListener(this);

		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getSherlockActivity());

		hostname = prefs.getString(getSherlockActivity().getString(R.string.key_seedbox_host), null);
		if (hostname != null) {
			port = Integer.parseInt(prefs.getString(getSherlockActivity().getString(R.string.key_seedbox_ftp_port),
					"21"));
			boolean identification = prefs.getBoolean(
					getSherlockActivity().getString(R.string.key_seedbox_use_identifiers), false);
			String user = prefs.getString(getSherlockActivity().getString(R.string.key_seedbox_login), null);
			String pass = prefs.getString(getSherlockActivity().getString(R.string.key_seedbox_password), null);

		}

		super.onActivityCreated(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.blue, null);

		mButton = (Button) view.findViewById(R.id.ftp_button);
		mButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				bind();
			}
		});
		return view;
	}

	private boolean bind() {
		return getActivity().bindService(new Intent(getActivity(), FTPService.class), connection,
				Context.BIND_AUTO_CREATE);
	}

	private void unbind() {
		getActivity().unbindService(connection);
	}

	private FTPService mFtpService;
	ServiceConnection connection = new ServiceConnection() {
		public void onServiceConnected(ComponentName name, IBinder binder) {
			Log.i("BackgroundService", "Connected!");
			mFtpService = (FTPService) ((BackgroundServiceBinder) binder).getService();
			mFtpService.getFTPManager().setFTPListener(FtpFragment.this);
			mFtpService.getFTPManager().connect("", 21);
		}

		public void onServiceDisconnected(ComponentName name) {
			Log.i("BackgroundService", "Disconnected!");
			mFtpService = null;
		}
	};

	@Override
	public void onDestroy() {
		mFtpService.getFTPManager().disconnect();
		unbind();
		mFtpService.stopSelf();
		super.onDestroy();
	}

	@Override
	public void onConnect(boolean connected) {
		Toast.makeText(getActivity(), "Connected ? " + connected, Toast.LENGTH_LONG).show();

	}

	@Override
	public void onAuthenticated(boolean success) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onDownloadFinished(int downloadPos) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onDownloadError(int errorCode, String downloadFailed, int downloadPos) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onDisconnected() {
		Toast.makeText(getActivity(), "Disconnected", Toast.LENGTH_LONG).show();

	}

	@Override
	public void onDirectories(FTPFile[] files, String directory) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onFiles(FTPFile[] files, String directory) {
		// TODO Auto-generated method stub

	}

}
